Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 569)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.96828 12.96772 12.96719 12.96668 12.96620 12.96573 12.96528 12.96483
##   [9] 12.96438 12.96393 12.96346 12.96299 12.96249 12.96198 12.96143 12.96085
##  [17] 12.96023 12.95956 12.95885 12.95808 12.95725 12.95636 12.95539 12.95436
##  [25] 12.95324 12.95204 12.95075 12.94936 12.94787 12.94629 12.94461 12.94286
##  [33] 12.94102 12.93912 12.93715 12.93513 12.93306 12.93095 12.92880 12.92663
##  [41] 12.92443 12.92221 12.91999 12.91777 12.91555 12.91334 12.91115 12.90899
##  [49] 12.90686 12.90477 12.90272 12.90072 12.89879 12.89692 12.89512 12.89340
##  [57] 12.89177 12.89011 12.88833 12.88642 12.88440 12.88228 12.88007 12.87777
##  [65] 12.87539 12.87295 12.87046 12.86791 12.86533 12.86272 12.86009 12.85745
##  [73] 12.85480 12.85217 12.84955 12.84695 12.84439 12.84188 12.83942 12.83702
##  [81] 12.83469 12.83245 12.83029 12.82824 12.82629 12.82446 12.82275 12.82119
##  [89] 12.81977 12.81850 12.81739 12.81646 12.81511 12.81279 12.80957 12.80555
##  [97] 12.80080 12.79539 12.78942 12.78296 12.77608 12.76888 12.76143 12.75381
## [105] 12.74610 12.73838 12.73073 12.72323 12.71596 12.70900 12.70244 12.69634
## [113] 12.69080 12.68588 12.68168 12.67827 12.67573 12.67414 12.67358 12.67413
## [121] 12.67571 12.67812 12.68131 12.68523 12.68981 12.69500 12.70073 12.70695
## [129] 12.71360 12.72062 12.72795 12.73554 12.74332 12.75124 12.75923 12.76725
## [137] 12.77522 12.78310 12.79082 12.79832 12.80556 12.81245 12.82088 12.83240
## [145] 12.84653 12.86278 12.88066 12.89968 12.91935 12.93919 12.95871 12.97742
## [153] 12.99482 13.01044 13.02377 13.03435 13.04390 13.05447 13.06598 13.07836
## [161] 13.09153 13.10543 13.11999 13.13512 13.15077 13.16685 13.18330 13.20005
## [169] 13.21701 13.23413 13.25132 13.26853 13.28566 13.30266 13.31945 13.33596
## [177] 13.35212 13.36785 13.38309 13.39776 13.41179 13.42510 13.43764 13.44931
## [185] 13.46006 13.46981 13.47849 13.48603 13.49235 13.49738 13.50106 13.50330
## [193] 13.50404 13.50321 13.50073 13.49654 13.49053 13.48277 13.47342 13.46261
## [201] 13.45050 13.43725 13.42300 13.40790 13.39210 13.37576 13.35903 13.34205
## [209] 13.32497 13.30796 13.29115 13.27470 13.25618 13.23349 13.20724 13.17806
## [217] 13.14656 13.11337 13.07910 13.04437 13.00981 12.97603 12.94366 12.91330
## [225] 12.88559 12.86115 12.83739 12.81147 12.78356 12.75386 12.72255 12.68981
## [233] 12.65583 12.62080 12.58491 12.54833 12.51127 12.47389 12.43639 12.39896
## [241] 12.36178 12.32504 12.28892 12.25361 12.21929 12.18616 12.15439 12.12418
## [249] 12.09571 12.06917 12.04473 12.02260 12.00295 11.98598 11.97179 11.96024
## [257] 11.95106 11.94400 11.93881 11.93525 11.93306 11.93199 11.93178 11.93220
## [265] 11.93298 11.93388 11.93464 11.93501 11.93475 11.93360 11.93131 11.92763
## [273] 11.92231 11.91769 11.91596 11.91664 11.91925 11.92329 11.92830 11.93379
## [281] 11.93928 11.94428 11.94831 11.95089 11.95154 11.94978 11.94512 11.93894
## [289] 11.93290 11.92698 11.92117 11.91544 11.90977 11.90413 11.89850 11.89287
## [297] 11.88720 11.88147 11.87567 11.86977 11.86375 11.85758 11.85124 11.84472
## [305] 11.83798 11.83101 11.82378 11.81627 11.80846 11.80032 11.79184 11.78299
## [313] 11.77374 11.76408 11.75399 11.74118 11.72386 11.70269 11.67833 11.65144
## [321] 11.62269 11.59274 11.56225 11.53190 11.50233 11.47422 11.44824 11.42503
## [329] 11.40527 11.38962 11.37875 11.36894 11.35642 11.34176 11.32552 11.30827
## [337] 11.29058 11.27301 11.25613 11.24050 11.22670 11.21528 11.20682 11.20188
## [345] 11.20102 11.20332 11.20739 11.21311 11.22039 11.22910 11.23915 11.25042
## [353] 11.26281 11.27621 11.29050 11.30558 11.32134 11.33767 11.35446 11.37161
## [361] 11.38900 11.40653 11.42409 11.44156 11.46102 11.48429 11.51089 11.54034
## [369] 11.57217 11.60590 11.64105 11.67715 11.71372 11.75028 11.78635 11.82147
## [377] 11.85514 11.88690 11.91626 11.94276 11.96962 12.00012 12.03381 12.07023
## [385] 12.10894 12.14948 12.19141 12.23428 12.27763 12.32103 12.36401 12.40614
## [393] 12.44695 12.48601 12.52286 12.55706 12.58814 12.61568 12.63920 12.66197
## [401] 12.68717 12.71427 12.74275 12.77208 12.80173 12.83118 12.85991 12.88738
## [409] 12.91307 12.93645 12.95699 12.97418 12.98747 12.99749 13.00527 13.01092
## [417] 13.01456 13.01632 13.01631 13.01465 13.01146 13.00685 13.00095 12.99388
## [425] 12.98575 12.97668 12.96679 12.95619 12.94502 12.93338 12.92139 12.90918
## [433] 12.89686 12.88455 12.87236 12.86043 12.84886 12.83777 12.82728 12.81752
## [441] 12.80860 12.79808 12.78381 12.76628 12.74601 12.72348 12.69922 12.67372
## [449] 12.64750 12.62104 12.59487 12.56948 12.54538 12.52307 12.50306 12.48586
## [457] 12.47196 12.45890 12.44409 12.42785 12.41052 12.39243 12.37392 12.35531
## [465] 12.33693 12.31912 12.30221 12.28653 12.27242 12.26019 12.25019 12.24153
## [473] 12.23311 12.22494 12.21705 12.20944 12.20213 12.19513 12.18847 12.18215
## [481] 12.17620 12.17062 12.16543 12.16065 12.15629 12.15236 12.14889 12.14588
## [489] 12.14336 12.14134 12.13982 12.13884 12.13840 12.13851 12.13920 12.14047
## [497] 12.14235 12.14485 12.14798 12.15177 12.15622 12.16130 12.16699 12.17326
## [505] 12.18008 12.18744 12.19531 12.20366 12.21246 12.22170 12.23135 12.24138
## [513] 12.25177 12.26249 12.27351 12.28482 12.29639 12.30819 12.32020 12.33239
## [521] 12.34492 12.35794 12.37143 12.38537 12.39976 12.41457 12.42978 12.44539
## [529] 12.46136 12.47768 12.49435 12.51133 12.52861 12.54618 12.56408 12.58236
## [537] 12.60102 12.62007 12.63950 12.65932 12.67952 12.70010 12.72107 12.74243
## [545] 12.76417 12.78630 12.80882 12.83173 12.85502 12.87870 12.90277 12.92723
## [553] 12.95208 12.97732 13.00295 13.02897 13.05539 13.08219 13.10939 13.13697
## [561] 13.16496 13.19333 13.22210 13.25126 13.28082 13.31077 13.34112 13.37187
## [569] 13.40301
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 569)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59624 12.59315 12.59014 12.58723 12.58440 12.58165 12.57898 12.57639
##   [9] 12.57386 12.57141 12.56902 12.56669 12.56441 12.56220 12.56003 12.55791
##  [17] 12.55584 12.55380 12.55181 12.54985 12.54791 12.54601 12.54413 12.54227
##  [25] 12.54042 12.53859 12.53677 12.53496 12.53315 12.53133 12.52949 12.52765
##  [33] 12.52580 12.52395 12.52211 12.52028 12.51848 12.51670 12.51495 12.51324
##  [41] 12.51157 12.50996 12.50839 12.50689 12.50546 12.50410 12.50282 12.50162
##  [49] 12.50052 12.49951 12.49861 12.49781 12.49713 12.49656 12.49613 12.49583
##  [57] 12.49566 12.49557 12.49548 12.49541 12.49535 12.49531 12.49530 12.49532
##  [65] 12.49538 12.49548 12.49563 12.49583 12.49609 12.49641 12.49681 12.49727
##  [73] 12.49781 12.49844 12.49915 12.49997 12.50088 12.50189 12.50301 12.50425
##  [81] 12.50561 12.50710 12.50872 12.51047 12.51236 12.51440 12.51660 12.51895
##  [89] 12.52146 12.52413 12.52699 12.53001 12.53298 12.53567 12.53810 12.54030
##  [97] 12.54230 12.54412 12.54579 12.54735 12.54881 12.55020 12.55155 12.55288
## [105] 12.55423 12.55562 12.55708 12.55862 12.56029 12.56211 12.56410 12.56629
## [113] 12.56871 12.57139 12.57434 12.57761 12.58121 12.58517 12.58953 12.59429
## [121] 12.59945 12.60492 12.61070 12.61676 12.62308 12.62964 12.63641 12.64338
## [129] 12.65053 12.65784 12.66528 12.67283 12.68048 12.68820 12.69597 12.70378
## [137] 12.71159 12.71940 12.72717 12.73489 12.74446 12.75746 12.77334 12.79158
## [145] 12.81164 12.83301 12.85513 12.87749 12.89955 12.92078 12.94064 12.95862
## [153] 12.97417 12.98677 12.99845 13.01153 13.02589 13.04143 13.05803 13.07560
## [161] 13.09402 13.11318 13.13297 13.15328 13.17400 13.19503 13.21625 13.23756
## [169] 13.25885 13.28000 13.30091 13.32146 13.34156 13.36109 13.37993 13.39799
## [177] 13.41515 13.43131 13.44635 13.46016 13.47264 13.48367 13.49315 13.50097
## [185] 13.50702 13.51119 13.51337 13.51344 13.51131 13.50685 13.50010 13.49123
## [193] 13.48041 13.46781 13.45359 13.43792 13.42096 13.40288 13.38385 13.36402
## [201] 13.34358 13.32268 13.30149 13.28017 13.25889 13.23782 13.21713 13.19697
## [209] 13.17752 13.15894 13.13824 13.11283 13.08343 13.05081 13.01570 12.97885
## [217] 12.94100 12.90291 12.86530 12.82894 12.79457 12.76293 12.73476 12.71081
## [225] 12.68695 12.65896 12.62746 12.59306 12.55640 12.51809 12.47874 12.43899
## [233] 12.39944 12.36072 12.32345 12.28825 12.25573 12.22653 12.20124 12.18051
## [241] 12.16233 12.14436 12.12665 12.10928 12.09230 12.07579 12.05980 12.04442
## [249] 12.02969 12.01570 12.00250 11.99016 11.97874 11.96832 11.95998 11.95456
## [257] 11.95179 11.95136 11.95301 11.95643 11.96134 11.96746 11.97450 11.98216
## [265] 11.99017 11.99824 12.00607 12.01338 12.01988 12.02529 12.02933 12.03169
## [273] 12.03209 12.03394 12.04030 12.05042 12.06351 12.07883 12.09559 12.11303
## [281] 12.13039 12.14690 12.16179 12.17430 12.18366 12.18910 12.18985 12.18754
## [289] 12.18434 12.18029 12.17542 12.16978 12.16341 12.15635 12.14864 12.14031
## [297] 12.13142 12.12199 12.11208 12.10172 12.09095 12.07981 12.06834 12.05659
## [305] 12.04459 12.03238 12.02001 12.00752 11.99494 11.98231 11.96968 11.95709
## [313] 11.94457 11.93218 11.91994 11.90549 11.88684 11.86461 11.83941 11.81186
## [321] 11.78256 11.75212 11.72117 11.69032 11.66017 11.63134 11.60445 11.58010
## [329] 11.55892 11.54150 11.52847 11.51638 11.50171 11.48502 11.46685 11.44773
## [337] 11.42821 11.40883 11.39013 11.37266 11.35694 11.34354 11.33298 11.32582
## [345] 11.32258 11.32215 11.32303 11.32514 11.32845 11.33290 11.33843 11.34499
## [353] 11.35253 11.36100 11.37034 11.38050 11.39143 11.40307 11.41537 11.42828
## [361] 11.44175 11.45572 11.47013 11.48495 11.50236 11.52423 11.54999 11.57908
## [369] 11.61091 11.64493 11.68057 11.71725 11.75441 11.79147 11.82787 11.86304
## [377] 11.89641 11.92740 11.95546 11.98001 12.00415 12.03112 12.06056 12.09213
## [385] 12.12548 12.16026 12.19611 12.23270 12.26967 12.30667 12.34335 12.37936
## [393] 12.41435 12.44798 12.47989 12.50974 12.53717 12.56183 12.58338 12.60427
## [401] 12.62695 12.65102 12.67611 12.70182 12.72779 12.75361 12.77891 12.80330
## [409] 12.82640 12.84783 12.86719 12.88412 12.89821 12.91022 12.92116 12.93106
## [417] 12.93994 12.94782 12.95473 12.96070 12.96574 12.96989 12.97317 12.97560
## [425] 12.97721 12.97802 12.97805 12.97734 12.97591 12.97378 12.97097 12.96752
## [433] 12.96344 12.95876 12.95350 12.94770 12.94137 12.93454 12.92723 12.91947
## [441] 12.91128 12.90116 12.88783 12.87171 12.85319 12.83268 12.81058 12.78729
## [449] 12.76320 12.73873 12.71428 12.69024 12.66701 12.64501 12.62462 12.60626
## [457] 12.59032 12.57350 12.55269 12.52860 12.50194 12.47341 12.44372 12.41357
## [465] 12.38367 12.35473 12.32745 12.30254 12.28071 12.26265 12.24908 12.23781
## [473] 12.22620 12.21433 12.20227 12.19009 12.17786 12.16564 12.15350 12.14152
## [481] 12.12976 12.11828 12.10717 12.09649 12.08631 12.07669 12.06771 12.05943
## [489] 12.05193 12.04527 12.03951 12.03474 12.03102 12.02842 12.02700 12.02684
## [497] 12.02800 12.03056 12.03458 12.04005 12.04687 12.05494 12.06418 12.07449
## [505] 12.08578 12.09797 12.11096 12.12467 12.13901 12.15388 12.16919 12.18486
## [513] 12.20080 12.21692 12.23312 12.24931 12.26542 12.28134 12.29699 12.31228
## [521] 12.32779 12.34413 12.36123 12.37904 12.39748 12.41651 12.43605 12.45605
## [529] 12.47644 12.49716 12.51815 12.53935 12.56069 12.58212 12.60378 12.62585
## [537] 12.64833 12.67122 12.69452 12.71822 12.74233 12.76685 12.79176 12.81708
## [545] 12.84280 12.86891 12.89542 12.92233 12.94963 12.97733 13.00542 13.03389
## [553] 13.06276 13.09201 13.12165 13.15168 13.18208 13.21287 13.24404 13.27559
## [561] 13.30752 13.33982 13.37250 13.40555 13.43898 13.47277 13.50694 13.54147
## [569] 13.57637
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 569)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.93869 11.93785 11.93707 11.93635 11.93569 11.93506 11.93448 11.93392
##   [9] 11.93339 11.93287 11.93237 11.93186 11.93135 11.93083 11.93029 11.92972
##  [17] 11.92912 11.92847 11.92778 11.92704 11.92624 11.92536 11.92441 11.92338
##  [25] 11.92226 11.92104 11.91972 11.91829 11.91674 11.91507 11.91326 11.91132
##  [33] 11.90923 11.90698 11.90458 11.90201 11.89926 11.89634 11.89327 11.89006
##  [41] 11.88672 11.88327 11.87972 11.87609 11.87240 11.86866 11.86488 11.86107
##  [49] 11.85726 11.85346 11.84968 11.84594 11.84225 11.83863 11.83509 11.83164
##  [57] 11.82831 11.82511 11.82204 11.81914 11.81640 11.81385 11.81150 11.80937
##  [65] 11.80705 11.80415 11.80072 11.79680 11.79241 11.78761 11.78244 11.77693
##  [73] 11.77112 11.76505 11.75876 11.75230 11.74570 11.73900 11.73223 11.72545
##  [81] 11.71869 11.71199 11.70539 11.69893 11.69264 11.68658 11.68077 11.67526
##  [89] 11.67008 11.66529 11.66091 11.65698 11.65355 11.65066 11.64834 11.64664
##  [97] 11.64559 11.64524 11.64561 11.64619 11.64645 11.64643 11.64618 11.64576
## [105] 11.64522 11.64460 11.64397 11.64336 11.64283 11.64243 11.64221 11.64223
## [113] 11.64253 11.64316 11.64417 11.64562 11.64755 11.65002 11.65308 11.65678
## [121] 11.66116 11.66628 11.67219 11.67894 11.68658 11.69516 11.70607 11.72038
## [129] 11.73765 11.75745 11.77937 11.80296 11.82780 11.85346 11.87952 11.90554
## [137] 11.93109 11.95574 11.97908 12.00065 12.02005 12.03684 12.05467 12.07701
## [145] 12.10314 12.13235 12.16393 12.19715 12.23130 12.26567 12.29954 12.33220
## [153] 12.36292 12.39100 12.41572 12.43636 12.45516 12.47476 12.49511 12.51611
## [161] 12.53771 12.55983 12.58239 12.60532 12.62855 12.65200 12.67561 12.69930
## [169] 12.72299 12.74661 12.77010 12.79337 12.81635 12.83898 12.86117 12.88286
## [177] 12.90396 12.92442 12.94415 12.96308 12.98113 12.99825 13.01434 13.02934
## [185] 13.04318 13.05579 13.06708 13.07698 13.08543 13.09236 13.09767 13.10131
## [193] 13.10320 13.10327 13.10145 13.09765 13.09153 13.08293 13.07208 13.05921
## [201] 13.04456 13.02835 13.01082 12.99220 12.97272 12.95261 12.93210 12.91142
## [209] 12.89081 12.87050 12.85072 12.83169 12.80954 12.78091 12.74683 12.70833
## [217] 12.66642 12.62215 12.57653 12.53060 12.48537 12.44188 12.40115 12.36420
## [225] 12.33208 12.30579 12.28202 12.25685 12.23041 12.20283 12.17424 12.14479
## [233] 12.11461 12.08382 12.05257 12.02098 11.98919 11.95734 11.92556 11.89397
## [241] 11.86273 11.83195 11.80177 11.77234 11.74377 11.71621 11.68978 11.66463
## [249] 11.64088 11.61868 11.59814 11.57942 11.56264 11.54793 11.53555 11.52552
## [257] 11.51762 11.51163 11.50731 11.50445 11.50282 11.50220 11.50236 11.50308
## [265] 11.50414 11.50532 11.50638 11.50711 11.50728 11.50668 11.50506 11.50222
## [273] 11.49793 11.49578 11.49895 11.50655 11.51769 11.53149 11.54706 11.56353
## [281] 11.58000 11.59559 11.60941 11.62059 11.62824 11.63146 11.62939 11.62358
## [289] 11.61628 11.60759 11.59759 11.58641 11.57413 11.56085 11.54668 11.53172
## [297] 11.51607 11.49982 11.48309 11.46596 11.44855 11.43094 11.41325 11.39557
## [305] 11.37800 11.36064 11.34360 11.32697 11.31086 11.29536 11.28058 11.26662
## [313] 11.25357 11.24154 11.23063 11.21824 11.20211 11.18281 11.16093 11.13707
## [321] 11.11179 11.08569 11.05935 11.03336 11.00831 10.98477 10.96333 10.94458
## [329] 10.92911 10.91749 10.91031 10.90597 10.90248 10.89980 10.89794 10.89689
## [337] 10.89662 10.89714 10.89842 10.90045 10.90323 10.90675 10.91098 10.91592
## [345] 10.92156 10.92877 10.93830 10.94997 10.96356 10.97889 10.99575 11.01394
## [353] 11.03327 11.05353 11.07454 11.09608 11.11797 11.13999 11.16196 11.18368
## [361] 11.20495 11.22556 11.24532 11.26404 11.28365 11.30602 11.33076 11.35754
## [369] 11.38598 11.41574 11.44645 11.47775 11.50929 11.54070 11.57164 11.60173
## [377] 11.63063 11.65797 11.68339 11.70655 11.72932 11.75371 11.77949 11.80646
## [385] 11.83439 11.86307 11.89229 11.92182 11.95146 11.98098 12.01018 12.03883
## [393] 12.06671 12.09363 12.11935 12.14366 12.16635 12.18720 12.20599 12.22485
## [401] 12.24577 12.26832 12.29209 12.31665 12.34158 12.36648 12.39091 12.41445
## [409] 12.43670 12.45722 12.47559 12.49141 12.50424 12.51474 12.52389 12.53175
## [417] 12.53837 12.54382 12.54816 12.55144 12.55373 12.55508 12.55556 12.55523
## [425] 12.55414 12.55236 12.54995 12.54696 12.54346 12.53950 12.53515 12.53047
## [433] 12.52551 12.52034 12.51501 12.50959 12.50414 12.49871 12.49337 12.48818
## [441] 12.48319 12.47698 12.46830 12.45747 12.44478 12.43056 12.41512 12.39876
## [449] 12.38180 12.36455 12.34732 12.33042 12.31417 12.29887 12.28484 12.27239
## [457] 12.26182 12.25147 12.23960 12.22648 12.21235 12.19749 12.18214 12.16657
## [465] 12.15102 12.13577 12.12106 12.10715 12.09430 12.08277 12.07281 12.06339
## [473] 12.05337 12.04281 12.03178 12.02037 12.00864 11.99668 11.98454 11.97231
## [481] 11.96006 11.94786 11.93580 11.92393 11.91233 11.90109 11.89027 11.87994
## [489] 11.87019 11.86108 11.85268 11.84508 11.83834 11.83254 11.82775 11.82404
## [497] 11.82150 11.82019 11.82019 11.82114 11.82264 11.82468 11.82727 11.83039
## [505] 11.83405 11.83825 11.84297 11.84822 11.85399 11.86029 11.86710 11.87443
## [513] 11.88226 11.89061 11.89946 11.90882 11.91867 11.92902 11.93986 11.95119
## [521] 11.96299 11.97525 11.98797 12.00116 12.01482 12.02897 12.04360 12.05873
## [529] 12.07437 12.09051 12.10717 12.12436 12.14207 12.16032 12.17910 12.19837
## [537] 12.21816 12.23845 12.25925 12.28056 12.30238 12.32470 12.34754 12.37089
## [545] 12.39475 12.41913 12.44402 12.46942 12.49534 12.52177 12.54872 12.57619
## [553] 12.60418 12.63268 12.66171 12.69125 12.72132 12.75191 12.78302 12.81466
## [561] 12.84682 12.87950 12.91271 12.94645 12.98071 13.01551 13.05083 13.08668
## [569] 13.12306
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")